package com.eastjet.maintenance.contrller;

import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.config.Result;
import com.eastjet.maintenance.domain.Allocate;
import com.eastjet.maintenance.domain.Ledger;
import com.eastjet.maintenance.service.impl.LedgerServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
备件帐台
 */
@RestController
@RequestMapping("/ledger")
@CrossOrigin(origins = "*")
public class LedgerController {

    @Autowired
    private LedgerServiceImpl ledgerService;// 注入service

    @PostMapping("/insertLedger")
    public Result InsertLedger(@RequestBody Map<String, Object> map) {
        Boolean b = ledgerService.InsertLedger(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "保存成功" : "保存失败");
    }

    /**
     * 查询账台
     *
     * @param ledger
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectLedger")
    public Result SelectLedger(Ledger ledger, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Ledger> ledgers = ledgerService.SelectLedger(ledger);
        PageInfo<Ledger> pageInfo = new PageInfo<>(ledgers);
        return new Result(pageInfo, ledgers.isEmpty() ? Code.GET_ERR : Code.GET_OK, ledgers.isEmpty() ? "查询结果为空" : "查询成功");
    }
}
